package com.sgb.kjwl.view.widget.self.goods;

import android.content.Context;
import android.os.Build;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import androidx.annotation.RequiresApi;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.sgb.kjwl.R;
import com.sgb.kjwl.model.entity.user.ShowWindowEntity;
import com.sgb.kjwl.model.entity.user.UnitEntity;
import com.sgb.kjwl.view.ui.adapter.self.goods.ShowVipPriceWindowAdpater;
import com.swgk.core.customview.ItemInputTextView;
import com.swgk.core.dialog.BasePopWindow;
import com.swgk.core.dialog.PopViewHolder;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 只做展示数据的底部PopWindow
 */
public class ShowVipPriceDetailWindow extends BasePopWindow {

    private String titles;
    private String lefttext;
    private String rightTest;

    private LinkedHashMap<String, String> map = new LinkedHashMap<>();


    private TextView okView;
    private TextView resetView;
    private TextView title;
    private RecyclerView recyclerView;

    private ShowVipPriceWindowAdpater madpater;

    public ShowVipPriceDetailWindow(Context context ) {
        super(context, DIR_DOWN_UP);
    }


    public void updateData(LinkedHashMap<String, String> map, String titles, String lefttext, String rightTest) {
        this.map = map;
        this.titles = titles;
        this.lefttext = lefttext;
        this.rightTest = rightTest;

        if (!TextUtils.isEmpty(titles)) {
            title.setText(titles);
        }
        if (!TextUtils.isEmpty(lefttext)) {
            resetView.setText(lefttext);
            resetView.setVisibility(View.VISIBLE);
        } else {
            resetView.setVisibility(View.GONE);
        }
        if (!TextUtils.isEmpty(rightTest)) {
            okView.setText(rightTest);
            okView.setVisibility(View.VISIBLE);
        } else {
            okView.setVisibility(View.GONE);
        }

        if (madpater != null) {
//            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
//                //使用lambda花里胡哨降低效率
//                madpater.setDatas(map.entrySet().stream().sorted(Comparator.comparing(e -> e.getKey())).map(e -> new ShowWindowEntity(e.getKey(), e.getValue())).collect(Collectors.toList()));
//
//            } else {//低版本兼容效率更高
            List<ShowWindowEntity> listData = new ArrayList<>();
            for (Map.Entry<String, String> stringStringEntry : map.entrySet()) {
                listData.add(new ShowWindowEntity(stringStringEntry.getKey(), stringStringEntry.getValue()));
            }
            madpater.setDatas(listData);
//            }
        }
    }

    @Override
    protected int popLayout() {
        return R.layout.popwindow_show_vip_price;
    }


    @Override
    protected void initView(PopViewHolder holder) {
        okView = holder.getPop().findViewById(R.id.ok_view);
        resetView = holder.getPop().findViewById(R.id.reset_view);
        title = holder.getPop().findViewById(R.id.title);
        recyclerView = holder.getPop().findViewById(R.id.recyclerview);
        okView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dissMiss();
                if (onResultClick != null) {
                    onResultClick.result(null, null, null);
                }
            }
        });

        madpater = new ShowVipPriceWindowAdpater(mContext, new ArrayList<>());
        recyclerView.setLayoutManager(new LinearLayoutManager(mContext));
        recyclerView.setAdapter(madpater);
        resetView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });
    }

}
